//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// 'Postal address items' dialog, 'Zips' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "PAItems.h"
# include "ListView.h"
# include "Map.h"
# include "ChangePAI.h"
# include "EditText.h"
# include "PropertyPages.h"
# include "Globals.h"

CZipsPage::CZipsPage () : CPropertyPages::CPage (IDD_PA_ZIPS) {}

void CZipsPage::_FillZips (zip_t * _pZip) {
	ListView_DeleteAllItems (m_List);

	CCritSec::CLock lock (g_cs);

	size_t cItem = 0;
	for (zips_t::iterator i = g_map.Zips.begin (); i != g_map.Zips.end (); ++ i) {
		const zip_t & zip = * i;
		if (zip.IsDeleted ())
			continue;

		InsertItem (m_List, cItem, reinterpret_cast<LPARAM> (& zip), "%s", OutputPAString (zip.strName));

		if (& zip == _pZip)
			SetSelection (m_List, cItem);

		++ cItem;
	}

	// Set the best width.
	AutoSizeColumns (m_List);

	m_List.Sort ();
}

void CZipsPage::_UpdateTotalZips () {
	size_t cZips = 0;
	for (zips_t::iterator i = g_map.Zips.begin (); i != g_map.Zips.end (); ++ i) {
		const zip_t & zip = * i;
		if (zip.IsDeleted ())
			continue;

		++ cZips;
	}

	char strTotal [32];
	::sprintf (strTotal, "Total: %d", cZips);

	SetDlgItemText (IDC_TOTAL, strTotal);
}

bool CZipsPage::OnInit (HWND _hwndFocus) {
	// Buttons.
	EnableDlgItem (IDC_REMOVE, false);
	EnableDlgItem (IDC_RENAME, false);

	// Zip codes.
	m_List.Init (m_hWnd, IDC_LIST, "ZipCodes");
	m_List.SetExtStyle (LVS_EX_FULLROWSELECT);

	AddColumn (m_List, 0, "Zip code");

	return true;
}

void CZipsPage::OnDestroy () {
	m_List.Destroy ();
}

void CZipsPage::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDC_ADD: {
			string_t strCode;
			if (EditText (m_hWnd, strCode, "Add zip code", false) && ! strCode.empty ()) {
				AddZip (strCode);
				_FillZips (& g_map.Zips.back ());
				_UpdateTotalZips ();
			}
			return;
		}

		case IDC_RENAME: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			zip_t * const pZip = reinterpret_cast<zip_t *> (GetLParam (m_List, cSel));

			string_t strName = pZip->strName;
			if (EditText (m_hWnd, strName, "Edit zip code", false)) {
				RenamePAI (pZip, strName);
				SetItemText (m_List, cSel, strName.c_str ());
			}

			return;
		}

		case IDC_REMOVE: {
			EnableDlgItem (IDC_RENAME, false);
			EnableDlgItem (IDC_REMOVE, false);

			const size_t cSel = ListView_GetSelectionMark (m_List);
			if (cSel == -1)
				break;

			zip_t * const pZip = reinterpret_cast<zip_t *> (GetLParam (m_List, cSel));
			DeletePAI (pZip);
			_FillZips ();
			_UpdateTotalZips ();

			return;
		}
	}
}

bool CZipsPage::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case NM_RCLICK:
			m_List.PopupMenu_OnHeaderRClick (_pInfo);
			return false;

		case LVN_COLUMNCLICK:
			m_List.Sort_OnColumnClick (_pInfo);
			break;

		case PSN_SETACTIVE: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			zip_t * const pZip = cSel == -1 ? NULL : reinterpret_cast<zip_t *> (GetLParam (m_List, cSel));
			_FillZips (pZip);
			_UpdateTotalZips ();
		}
		case NM_CLICK:
		case LVN_KEYDOWN: {
			const size_t cSel = ListView_GetSelectionMark (m_List);
			EnableDlgItem (IDC_RENAME, cSel != -1);
			EnableDlgItem (IDC_REMOVE, cSel != -1);
			break;
		}
	}

	return CPage::OnNotify (_idCtrl, _pInfo);
}
